package com.game.modular.game.battle.info.roundInfo.dto;

import com.game.modular.game.playerAssets.domain.PlayerHero;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.*;
import java.util.stream.Collectors;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class PvpBattleStatisticsPlayerInfo {
    // 攻击方玩家id
    private Long playerId;
    // 战前总兵力
    private Long totalForceBefore;
    // 战后兵力
    private Long totalForceAfter;
    private Set<HeroInfo> heroInfoList;

    public static PvpBattleStatisticsPlayerInfo init(List<PlayerHero> playerHeroList) {
        Long uid = playerHeroList.get(0).getUid();
        long totalForceBefore = playerHeroList.stream().mapToLong(PlayerHero::getCurrentForce).sum();
        Set<HeroInfo> collect = playerHeroList.stream().map(HeroInfo::init).collect(Collectors.toSet());

        return PvpBattleStatisticsPlayerInfo.builder().playerId(uid).totalForceBefore(totalForceBefore).heroInfoList(collect).build();
    }

    public void setHeroInfo(HeroInfo heroInfo){
        Long playerHeroId = heroInfo.getPlayerHeroId();
        heroInfoList.removeIf(item -> item.getPlayerHeroId().equals(playerHeroId));
        heroInfoList.add(heroInfo);
    }
    public HeroInfo getHeroInfo(Long playerHeroId) {
        // 假设 coreSkillId 已经定义好
        Optional<HeroInfo> result = heroInfoList.stream()
                .filter(item -> item != null && item.getPlayerHeroId() != null && item.getPlayerHeroId().equals(playerHeroId))
                .findFirst();
        return result.orElse(null);
    }
    public HeroInfo.SkillInfo getSkillInfo(Long playerHeroId , Long coreSkillStarId) {
        return getHeroInfo(playerHeroId).getSkillInfo(coreSkillStarId);
    }
    public void setSkillInfo(Long playerHeroId , HeroInfo.SkillInfo skillInfo)  {
        HeroInfo heroInfo = this.getHeroInfo(playerHeroId);
        heroInfo.setSkillInfo(skillInfo);
    }


    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    public static class HeroInfo {
        @ApiModelProperty("队伍位置")
        private Integer rankOrder;
        @ApiModelProperty("武将id")
        private Long playerHeroId;
        @ApiModelProperty("武将基础id")
        private Long coreHeroId;
        @ApiModelProperty("战前兵力")
        private Long forceBefore;
        @ApiModelProperty("战后兵力")
        private Long forceAfter;
        @ApiModelProperty("丢失兵力")
        private Long forceLost;
        @ApiModelProperty("击杀数")
        private Long killCount;
        @ApiModelProperty("连携信息id")
        private Long relationId;
        @ApiModelProperty("所属阵营")
        private Long campId;
        @ApiModelProperty("普通击杀数")
        private Long oAKillCount;
        @ApiModelProperty("总恢复")
        private Long recoverCount;
        @ApiModelProperty("技能释放信息")
        private Set<SkillInfo> skillInfoList;

        public static HeroInfo init(PlayerHero item) {
            return HeroInfo.builder()
                    .rankOrder(item.getRankOrder())
                    .playerHeroId(item.getId())
                    .coreHeroId(item.getCoreHeroId())
                    .forceBefore(item.getCurrentForce())
                    .forceAfter(item.getCurrentForce())
                    .forceLost(0L)
                    .killCount(0L)
                    .relationId(0L)
                    .campId(null)
                    .oAKillCount(0L)
                    .skillInfoList(SkillInfo.init(item))
                    .build();
        }

        public SkillInfo getSkillInfo(Long coreSkillStarId) {
            // 假设 coreSkillId 已经定义好
            Optional<SkillInfo> result = skillInfoList.stream()
                    .filter(item -> item != null && item.getCoreSkillStarId() != null && item.getCoreSkillStarId().equals(coreSkillStarId))
                    .findFirst();
            return result.orElse(null);
        }

        public void setSkillInfo(SkillInfo skillInfo) {
            Long coreSkillStarId = skillInfo.getCoreSkillStarId();
            skillInfoList.removeIf(item -> item.getCoreSkillStarId().equals(coreSkillStarId));
            skillInfoList.add(skillInfo);
        }


        @Data
        @AllArgsConstructor
        @NoArgsConstructor
        @Builder
        public static class SkillInfo {
            @ApiModelProperty("武将id")
            private Long playerHeroId;
            @ApiModelProperty("普通技能id")
            private Long coreSkillId;
            @ApiModelProperty("普通技能星级id")
            private Long coreSkillStarId;
            @ApiModelProperty("释放次数")
            private Integer count;
            @ApiModelProperty("击杀数")
            private Long killCount;
            @ApiModelProperty("恢复数")
            private Long recoverCount;

            public static Set<SkillInfo> init(PlayerHero item) {
                Set<SkillInfo> skillInfos = new HashSet<>();
                if (item != null) {
                    for (Long skillStarId : item.getSkillStarIds()) {
                        SkillInfo build = SkillInfo.builder()
                                .playerHeroId(item.getId())

                                // @todo bug
                                .coreSkillId(item.getSkill())

                                .coreSkillStarId(skillStarId)
                                .count(0)
                                .killCount(0L)
                                .recoverCount(0L)
                                .build();
                        skillInfos.add(build);
                    }
                }
                return skillInfos;
            }
        }
    }

}
